package gold.digger;

import gold.utils.InputUtil;

import java.util.TreeMap;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LCIT1606 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int smallestDifference(int[] a, int[] b) {
            TreeMap<Integer, Integer> map = new TreeMap<>();

            for (int i = 0; i < a.length; i++) {
                map.put(a[i], 1);
            }

            for (int i = 0; i < b.length; i++) {
                if (map.containsKey(b[i])) return 0;
                map.put(b[i], 2);
            }

            long minDiff = Integer.MAX_VALUE, aNum = 0, bNum = 0;
            boolean aFlag = false, bFlag = false;
            for (Integer num : map.keySet()) {
                if (map.get(num) == 1) {
                    aNum = num;
                    aFlag = true;
                    if (aFlag && bFlag) minDiff = Math.min(minDiff, Math.abs(bNum - aNum));
                    continue;
                }

                if (map.get(num) == 2) {
                    bNum = num;
                    bFlag = true;
                    if (aFlag && bFlag) minDiff = Math.min(minDiff, Math.abs(bNum - aNum));
                    continue;
                }
            }

            return ((int) minDiff);
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] a = InputUtil.toIntegerArray("[-2147483648,1]");
        int[] b = InputUtil.toIntegerArray("[2147483647,0]");
        System.out.println(solution.smallestDifference(a, b));
    }

    public static void main(String[] args) throws Exception {
        LCIT1606 an = new LCIT1606();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
